package com.yuanqi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanqi.common.Result;
import com.yuanqi.pojo.Sku;
import com.yuanqi.service.SkuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * SKU管理接口
 */
@RestController
@RequestMapping("/api/sku")
@RequiredArgsConstructor
public class SkuController {

    private final SkuService skuService;

    @PostMapping
    public Result<Boolean> create(@RequestBody Sku sku) {
        return Result.success(skuService.save(sku));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody Sku sku) {
        return Result.success(skuService.updateById(sku)); // 修改
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(skuService.removeById(id)); // 删除
    }

    @GetMapping("/page")
    public Result<Page<Sku>> page(@RequestParam(required = false) Long productId,
                                  @RequestParam(defaultValue = "1") Integer pageNo,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<Sku> qw = new QueryWrapper<>();
        if (productId != null) {
            qw.eq("product_id", productId);
        }
        qw.orderByDesc("created_at");
        return Result.success(skuService.page(new Page<>(pageNo, pageSize), qw));
    }
}


